# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realName=RuC
_realname=${_realName,,}
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.5.1
pkgrel=1
pkgdesc="Yet another compiler from Russian version of C into virtual machine (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url='https://github.com/andrey-terekhov/RuC'
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
optdepends=("${MINGW_PACKAGE_PREFIX}-ruc-vm: For default output interpretation")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("${_realName}-${pkgver}.tar.gz::${url}/archive/v${pkgver}.tar.gz"
        '0002-ruc-2.4.3-gnu-install-dirs.patch'
        '0003-ruc-2.4.3-libruc-prefix.patch'
        '0004-ruc-2.4.3-codegen-env.patch')
sha256sums=('8c83308bf28312d67e6672e3e4c6b9174810ce124bb696697167fa7f0eaa9d50'
            'bb6f6f8824b008e38a388ca74d5698dedc7864282960830982df6514e34cec7f'
            'd27579d289c975dfa48375c215013a12977097ef9dad6c2547e3a9107894b486'
            '6a11f0bf10c4237d1cc09447a59070b905530fa6b3d6fb2df0b5d649808881b3')

prepare() {
  cd "${srcdir}"/${_realName}-${pkgver}

  patch -Np1 -i "${srcdir}"/0002-ruc-2.4.3-gnu-install-dirs.patch
  patch -Np1 -i "${srcdir}"/0003-ruc-2.4.3-libruc-prefix.patch
  patch -Np1 -i "${srcdir}"/0004-ruc-2.4.3-codegen-env.patch
}

build() {
  cd "${srcdir}/${_realName}-${pkgver}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      ../${_realName}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
